//
//  TicketInfoCtrl.m
//  Traveling
//
//  Created by Northwan on 15/6/11.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "TicketInfoCtrl.h"
#import "CitySelectCtrl.h"
#import "DateSelecter.h"
#import "TimeUtils.h"
#import "TrainListCtrl.h"
#import "CustomShowView.h"
#import "PlaneListCtrl.h"
#import "SelectDateView.h"

#define StartDateFormat @"yyyy-MM-dd"

@interface TicketInfoCtrl ()<DateSelecterDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bannerImgView;
@property (weak, nonatomic) IBOutlet UIButton *startCityBtn;
@property (weak, nonatomic) IBOutlet UIButton *endCityBtn;
@property (weak, nonatomic) IBOutlet UIButton *startDateBtn;
@property (assign, nonatomic)TicketSelectViewClickType type;
@property (nonatomic, strong) CustomShowView *showView;

@end

@implementation TicketInfoCtrl

-(instancetype)initWithType:(TicketSelectViewClickType)type{
    self = [super init];
    if(self){
       self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dealBannerImg];
    
    [self.startDateBtn setTitle:[TimeUtils getDateStringWithDate:[NSDate date] format:StartDateFormat] forState:UIControlStateNormal];
}

-(void)dealBannerImg{
    switch (self.type) {
        case TicketSelectViewClickTypeAir:
           [self.navigationItem setTitle:@"国内机票"];
           [self.bannerImgView setImage:[UIImage imageNamed:@"ticket_airport_banner"]];
            break;
        case TicketSelectViewClickTypeTrain:
           [self.navigationItem setTitle:@"高铁查询"];
            [self.bannerImgView setImage:[UIImage imageNamed:@"ticket_train_banner"]];
            break;
        case TicketSelectViewClickTypeCar:
           [self.navigationItem setTitle:@"汽车票查询"];
            [self.bannerImgView setImage:[UIImage imageNamed:@"ticket_car_banner"]];
            break;
        default:
            break;
    }
}

- (IBAction)btnTapAction:(UIButton *)sender {
    
    switch (sender.tag) {
        case 0:{
            NSLog(@"出发城市");
            [self.navigationController pushViewController:[[CitySelectCtrl alloc] initWithType:self.type result:^(NSString *city) {
                //返回的城市
                [self.startCityBtn setTitle:city forState:UIControlStateNormal];
            }] animated:YES];
            break;
        }
        case 1:{
            NSLog(@"到达城市");
            [self.navigationController pushViewController:[[CitySelectCtrl alloc] initWithType:self.type result:^(NSString *city) {
                //返回的城市
                [self.endCityBtn setTitle:city forState:UIControlStateNormal];
            }] animated:YES];
            break;
        }
        case 2:{
            NSLog(@"出发时间");
            SelectDateView *dateView = [SelectDateView dateViewWithResult:^(NSString *dateStr, NSDate *date) {
                [self.showView hideWithanim:NO];
                [self selectedWithDate:date];
            }];
            dateView.width = SCREENW-16;
            dateView.height = 254;
            CustomShowView *showView = [[CustomShowView alloc] initWithCustomView:dateView];
            [showView setBackgroundColor:RGBAColor(0, 0, 0, 0.5)];
            self.showView = showView;
            [self.showView showWithanim:YES];
            break;
        }
        case 3:{
            if (self.type == TicketSelectViewClickTypeTrain) {
                NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
                fmt.dateFormat = @"yyyy-MM-dd";
                NSDate *date = [fmt dateFromString:self.startDateBtn.currentTitle];
                TrainListCtrl *ctrl = [[TrainListCtrl alloc] initWithStartCity:self.startCityBtn.currentTitle entCity:self.endCityBtn.currentTitle date:date];
                [self.navigationController pushViewController:ctrl animated:YES];
            }else if(self.type == TicketSelectViewClickTypeAir){
                NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
                fmt.dateFormat = @"yyyy-MM-dd";
                NSDate *date = [fmt dateFromString:self.startDateBtn.currentTitle];
                PlaneListCtrl *ctrl = [[PlaneListCtrl alloc] initWithStartCity:self.startCityBtn.currentTitle entCity:self.endCityBtn.currentTitle date:date];
                [self.navigationController pushViewController:ctrl animated:YES];
            }
            break;
        }
        default:
            break;
    }
}

- (void)selectedWithDate:(NSDate *)date{
    [self.startDateBtn setTitle:[TimeUtils getDateStringWithDate:date format:StartDateFormat] forState:UIControlStateNormal];
}

@end
